package com.wb.service;

import com.google.common.collect.Lists;
import com.wb.dao.user.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import com.wb.bean.user.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service(value = "userDetailServiceImpl")
public class UserDetailServiceImpl implements UserDetailsService {

    private final Logger logger = LoggerFactory.getLogger(UserDetailServiceImpl.class);

    @Resource(name = "userMapper")
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        User user = null;
        try {
            user = userMapper.queryUserByUsreName(new User(userName,"",null));
        } catch (Exception e) {
            logger.error("" , e);
        }
        List<GrantedAuthority> authorities = Lists.newArrayList(
                new SimpleGrantedAuthority("admin"),
                new SimpleGrantedAuthority("admin1"),
                new SimpleGrantedAuthority("button"));
        user.setAuthorities(authorities);
        logger.info(" ------- user: {}" , user);
        return user;
    }
}